package com.ruoyi.industryfrontend.service.impl;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.uuid.IdUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.industryfrontend.domain.ProjectPark;
import com.ruoyi.industryfrontend.domain.ProjectWorkshop;
import com.ruoyi.industryfrontend.domain.SpaceIndustry;
import com.ruoyi.industryfrontend.mapper.*;
import com.ruoyi.industryfrontend.service.IProjectWorkshopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 厂房Service业务层处理
 *
 * @author MagicCube
 * @date 2023-05-08
 */
@Service
public class ProjectWorkshopServiceImpl implements IProjectWorkshopService
{
    @Autowired
    private ProjectWorkshopMapper projectWorkshopMapper;

    @Autowired
    private DistrictMapper districtMapper;

    @Autowired
    private ProjectParkMapper projectParkMapper;

    @Autowired
    private SpaceIndustryMapper spaceIndustryMapper;

    @Autowired
    private SpaceCollectMapper spaceCollectMapper;
    /**
     * 查询厂房
     *
     * @param id 厂房主键
     * @return 厂房
     */
    @Override
    public ProjectWorkshop selectProjectWorkshopById(Long id)
    {
        return projectWorkshopMapper.selectProjectWorkshopById(id);
    }

    /**
     * 根据编号查询厂房
     * @param spaceId 编号
     * @return 结果
     */
    @Override
    public Map<String, Object> selectProjectWorkshopByNumber(String spaceId) {
        ProjectWorkshop projectWorkshop = projectWorkshopMapper.selectProjectWorkshopByNumber(spaceId);
        List<String> list = spaceCollectMapper.selectSpaceCollectByUserId(SecurityUtils.getUserId(),spaceId);
        if (list!=null&&list.size()!=0){
            projectWorkshop.setCollectState(1L);
        }else {
            projectWorkshop.setCollectState(0L);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("space",projectWorkshop);
        if (projectWorkshop.getParkId()!=null) {
            ProjectPark projectPark = projectParkMapper.selectProjectParkById(projectWorkshop.getParkId());
            map.put("park",projectPark);
        }
        return map;
    }

    /**
     * 查询厂房列表
     *
     * @param projectWorkshop 厂房
     * @return 厂房
     */
    @Override
    public List<ProjectWorkshop> selectProjectWorkshopList(ProjectWorkshop projectWorkshop)
    {
//        List<Long> districtIds = new ArrayList<Long>();
//        //城市
//        if (projectWorkshop.getCityId()!=null){
//            districtIds.add(projectWorkshop.getCityId());
//            projectWorkshop.setCityIds(districtIds);
//        }else if (projectWorkshop.getCityId()==null && projectWorkshop.getProvinceId() != null){
//            //查询省份下的城市
//            districtIds.addAll(districtMapper.selectDistrictIdList(projectWorkshop.getProvinceId()));
//            districtIds.add(projectWorkshop.getProvinceId());
//            projectWorkshop.setCityIds(districtIds);
//        }
        //应急系统
        if (projectWorkshop.getWshopEmergency() != null && projectWorkshop.getWshopEmergency().equals("H")) {
            projectWorkshop.setWshopEmergency("有");
        } else if (projectWorkshop.getWshopEmergency() != null && projectWorkshop.getWshopEmergency().equals("N")) {
            projectWorkshop.setWshopEmergency("无");
        }
        //出售价格
        if (projectWorkshop.getWshopSellPrice()!=null){
            String[] split = projectWorkshop.getWshopSellPrice().split("~");
            projectWorkshop.setWshopSellPricelow(Long.parseLong(split[0]));
            if (!split[1].equals("N")) {
                projectWorkshop.setWshopSellPriceHigh(Long.parseLong(split[1]));
            }
        }
        //租赁价格
        if(projectWorkshop.getWshopLeasePrice()!=null){
            String[] split = projectWorkshop.getWshopLeasePrice().split("~");
            projectWorkshop.setWshopSellPricelow(Long.parseLong(split[0]));
            if (!split[1].equals("N")) {
                projectWorkshop.setWshopSellPriceHigh(Long.parseLong(split[1]));
            }
        }
        projectWorkshop.setSupplyUserId(SecurityUtils.getUserId());
        return projectWorkshopMapper.selectProjectWorkshopListz(projectWorkshop);
    }

    /**
     * 新增厂房
     *
     * @param projectWorkshop 厂房
     * @return 结果
     */
    @Override
    public int insertProjectWorkshop(ProjectWorkshop projectWorkshop)
    {
        projectWorkshop.setSupplyUserId(SecurityUtils.getUserId());
        projectWorkshop.setApprovalState(0L);
        projectWorkshop.setCreateTime(DateUtils.getNowDate());
        projectWorkshop.setSerialNumber(IdUtils.fastSimpleUUID());

        SpaceIndustry spaceIndustry = new SpaceIndustry();
        extracted(projectWorkshop, spaceIndustry);
        spaceIndustry.setSupplyUserId(SecurityUtils.getUserId());
        spaceIndustry.setCreateBy(SecurityUtils.getUsername());
        spaceIndustry.setCreateTime(projectWorkshop.getCreateTime());
        spaceIndustry.setSpaceId(projectWorkshop.getSerialNumber());
        spaceIndustryMapper.insertSpaceIndustry(spaceIndustry);

        return projectWorkshopMapper.insertProjectWorkshop(projectWorkshop);
    }

    private static void extracted(ProjectWorkshop projectWorkshop, SpaceIndustry spaceIndustry) {
        spaceIndustry.setBuildType(5);
        spaceIndustry.setReleaseType(0L);//供给
        spaceIndustry.setApprovalState(projectWorkshop.getApprovalState());
        spaceIndustry.setProvinceId(projectWorkshop.getProvinceId());//省份
        spaceIndustry.setCityId(projectWorkshop.getCityId());//城市
//        spaceIndustry.setAreaId();//区域
        spaceIndustry.setSupplyType(projectWorkshop.getSupplyType());//服务类型
        spaceIndustry.setParkId(String.valueOf(projectWorkshop.getParkId()));
        spaceIndustry.setHeating(projectWorkshop.getWshopHeating());//供暖
        spaceIndustry.setRefrig(projectWorkshop.getWshopRefrig());//制冷
        spaceIndustry.setPower(projectWorkshop.getWshopPower());//电力
        spaceIndustry.setGas(projectWorkshop.getWshopGas());//燃气
        spaceIndustry.setLeasePrice(projectWorkshop.getWshopLeasePrice());//出售价格
        spaceIndustry.setSellPrice(projectWorkshop.getWshopSellPrice());//租赁价格
        spaceIndustry.setCoverResource(projectWorkshop.getCoverResource());//封面资源
        spaceIndustry.setSpaceName(projectWorkshop.getWshopName());//园区名称
        spaceIndustry.setSpaceAllSize(projectWorkshop.getWshopAllSize());//空间总面积
        spaceIndustry.setTop(projectWorkshop.getTop());//置顶
    }

    /**
     * 修改厂房
     *
     * @param projectWorkshop 厂房
     * @return 结果
     */
    @Override
    public int updateProjectWorkshop(ProjectWorkshop projectWorkshop)
    {
        projectWorkshop.setApprovalState(0L);//未审核
        projectWorkshop.setUpdateTime(DateUtils.getNowDate());
        SpaceIndustry spaceIndustry = new SpaceIndustry();
        extracted(projectWorkshop,spaceIndustry);
        spaceIndustry.setSpaceId(String.valueOf(projectWorkshop.getId()));
        spaceIndustry.setUpdateBy(SecurityUtils.getUsername());
        spaceIndustry.setUpdateTime(projectWorkshop.getUpdateTime());
        spaceIndustryMapper.updateSpaceIndustryByWorkshopId(spaceIndustry);
        return projectWorkshopMapper.updateProjectWorkshop(projectWorkshop);
    }

    /**
     * 批量删除厂房
     *
     * @param ids 需要删除的厂房主键
     * @return 结果
     */
    @Override
    public int deleteProjectWorkshopByIds(Long[] ids)
    {
        return projectWorkshopMapper.deleteProjectWorkshopByIds(ids);
    }

    /**
     * 删除厂房信息
     *
     * @param id 厂房主键
     * @return 结果
     */
    @Override
    public int deleteProjectWorkshopById(Long id)
    {
        spaceIndustryMapper.deleteSpaceIndustryByProjectParkId(id);
        return projectWorkshopMapper.deleteProjectWorkshopById(id);
    }

    @Override
    public int deleteProjectWorkshopBySerialNumber(String spaceId) {
        spaceIndustryMapper.deleteSpaceIndustryBySerialNumber(spaceId);
        return projectWorkshopMapper.deleteProjectWorkshopBySerialNumber(spaceId);
    }
}
